//
//  AppDelegate.m
//  Funny
//
//  Created by dllo on 15/10/26.
//  Copyright © 2015年 King. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) BMKMapManager* mapManager;
@property (nonatomic, strong) GuideViewController *guideVC;
@property (nonatomic, strong) UITabBarController *tab;

@end

@implementation AppDelegate

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    self.mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [self.mapManager start:@"2kZhZDpOxcZ8nzLfyOtYOl0N" generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    

        [ShareSDK registerApp:@"iosv1101"
    activePlatforms:@[
                      @(SSDKPlatformTypeSinaWeibo),
                      @(SSDKPlatformTypeTencentWeibo),
                      @(SSDKPlatformTypeWechat),
                      ]
    onImport:^(SSDKPlatformType platformType) {
        switch (platformType)
        {
            case SSDKPlatformTypeWechat:
                [ShareSDKConnector connectWeChat:[WXApi class]];
                break;
            case SSDKPlatformTypeSinaWeibo:
                [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                break;
            default:
                break;
        }
    }
    onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
        switch (platformType)
        {
            case SSDKPlatformTypeSinaWeibo:
                //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                [appInfo SSDKSetupSinaWeiboByAppKey:@"1105156638"
                                          appSecret:@"71ab0a5cc40749c40b9b01e86872a665"
                                        redirectUri:@"http://www.sharesdk.cn"
                                           authType:SSDKAuthTypeBoth];
                break;
            case SSDKPlatformTypeTencentWeibo:
                //设置腾讯微博应用信息，其中authType设置为只用Web形式授权
                [appInfo SSDKSetupTencentWeiboByAppKey:@"801307650"
                                             appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                           redirectUri:@"http://www.sharesdk.cn"];
                break;
            case SSDKPlatformTypeWechat:
                [appInfo SSDKSetupWeChatByAppId:@"wx28345711d25e2863"
                                      appSecret:@"d4624c36b6795d1d99dcf0547af5443d"];
                break;
            default:
                break;
        }
    }];
    
    self.guideVC = [[GuideViewController alloc]init];
    self.tab = [[UITabBarController alloc]init];
    
    MainViewController *mainVC = [[MainViewController alloc]init];
    mainVC.tabBarItem.title = @"首页";
    mainVC.tabBarItem.image = [UIImage imageNamed:@"shouye@3x.png"];
    UINavigationController *mainNVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
    
    FindViewController *findVC = [[FindViewController alloc]init];
    findVC.tabBarItem.title = @"周边";
    findVC.tabBarItem.image = [UIImage imageNamed:@"zhoubian@3x.png"];
    UINavigationController *findNVC = [[UINavigationController alloc]initWithRootViewController:findVC];
    
    InlandViewController *inlandVC = [[InlandViewController alloc]init];
    inlandVC.tabBarItem.title = @"国内";
    inlandVC.tabBarItem.image = [UIImage imageNamed:@"lvxing@3x.png"];
    UINavigationController *inlandNVC = [[UINavigationController alloc]initWithRootViewController:inlandVC];
    
    MyViewController *myVC = [[MyViewController alloc]init];
    myVC.tabBarItem.title = @"我的";
    myVC.tabBarItem.image = [UIImage imageNamed:@"wode@3x.png"];
    UINavigationController *myNVC = [[UINavigationController alloc]initWithRootViewController:myVC];
    
    self.tab.viewControllers = @[mainNVC,findNVC,inlandNVC,myNVC];
    self.tab.tabBar.tintColor = [UIColor colorWithRed:0 green:220.0 / 256.0 blue:220.0 / 256.0 alpha:1.0];

    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"last"] == nil) {
        self.window.rootViewController = self.guideVC;
    }else{
        self.window.rootViewController = self.tab;
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(guide:) name:@"guide" object:nil];
    
    // Override point for customization after application launch.
    return YES;
}

- (void)guide:(NSNotification *)not
{
    [UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.guideVC.view.alpha = 0;
    } completion:^(BOOL finished) {
        [self.guideVC removeFromParentViewController];
        self.window.rootViewController = self.tab;
    }];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
    [BMKMapView willBackGround];
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
   
    [BMKMapView didForeGround];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}

@end
